package com.laomeng.client.service.Impl;

import com.grpc.lib.hello.HelloRequest;
import com.grpc.lib.hello.HelloResponse;
import com.grpc.lib.hello.HelloServiceGrpc;
import com.laomeng.client.service.IHelloService;

import io.grpc.Channel;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase implements IHelloService {

    private static final Logger LOG = LoggerFactory.getLogger(HelloServiceImpl.class);

    @GrpcClient("local-grpc-server")
    private Channel channel;

    @Override
    public Map<String,Object> sayHello(String name) {

        LOG.info("开启管道");
        HelloServiceGrpc.HelloServiceBlockingStub blockingStub = HelloServiceGrpc.newBlockingStub(channel);
        HelloResponse response = blockingStub.sayHello(HelloRequest.newBuilder().setName(name).build());

        LOG.info("数据返回:{}",response);

        Map<String,Object> map = new HashMap<>(2);
        map.put("code", response.getCode());
        map.put("msg", response.getMessage());
        return map;
    }
}

